﻿/***********************************************************************
 * <copyright file="CustomerController.cs" company="BSD JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, May 16, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;
using DevExpress.XtraReports.UI;
using DxMvcApplication.Areas.Administrator.Controllers.Report;
using DxMvcApplication.Areas.Administrator.Models.Dictionary;
using DxMvcApplication.Areas.Administrator.Report;
using DxMvcApplication.Repositories.Implementations.Dictionary;

namespace DxMvcApplication.Areas.Administrator.Controllers.Dictionary
{
    /// <summary>
    /// CustomerController controller
    /// </summary>
    public class CustomerController : BaseReportController
    {
        private readonly CustomerRepository _customerRepository;

        public override XtraReport GetReport(Models.Report.ReportCriteria criteria)
        {
            //var result = _customerRepository.GetPaged(1).ToModel();
            var totalPage = 0;
            var result = _customerRepository.GetPaged(ref totalPage, 1);
            int number = 0;
            foreach (var item in result)
            {
                number++;
                item.CustomerId += number;
            }
            DataSource = result;
            Report = new CustomerReport(DataSource);
            //PromulgateInfo = "22323";
            ControllerName = "Customer";
            ReportTitle = "DANH SÁCH KHÁCH HÀNG";
            return Report;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomerController"/> class.
        /// </summary>
        public CustomerController()
        {
            _customerRepository = new CustomerRepository();
            IsPrintDictionary = true;
        }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public override ActionResult Index(int? siteId = 0)
        {
            return View("Customer");
        }

        public PartialViewResult GetPaged(int? page)
        {
            var pageIndex = page ?? 1;
            var totalPage = _customerRepository.GetPageSize();
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = pageIndex;
            var customers = _customerRepository.GetPaged(ref totalPage, pageIndex);
            return PartialView("CustomerList", customers.ToModels());
        }

        public ActionResult Detail(int? id)
        {
            if (id > 0)
            {
                var customer = _customerRepository.Get((int)id);
                return View("CustomerDetail", customer.ToModel());
            }
            return View("CustomerDetail", new CustomerModel());
        }
        [HttpPost]
        public JsonResult SaveData(CustomerModel model)
        {
            string msg = null;
            if (ModelState.IsValid)
            {
                msg = model.CustomerId > 0 ? _customerRepository.Update(model.FromModel()) : _customerRepository.Insert(model.FromModel());
            }
            return Json(new { message = msg });
        }
        [HttpPost]
        public JsonResult Delete(int? id)
        {
            string msg = null;
            if (id > 0)
            {
                msg = _customerRepository.Delete((int)id);
            }
            return Json(new { message = msg }, JsonRequestBehavior.AllowGet);
        }
    }
}
